﻿(function ($) {

    function init(target) {
        var opt = $.data(target, "wlUpload").options;
        //if (opt.guid == "") {
        //    opt.guid = utils.newGuid();
        //}
        //else {
        //    $(target).wlUpload("GetFileList");
        //}

        if (opt.guid) {
            $(target).wlUpload("GetFileList");

        }
        else {
            opt.guid = utils.newGuid();
        }

        var table = "   <form id='" + opt.guid + "' action='" + opt.url + "' method='" + opt.method + "' enctype='multipart/form-data'>";
        table += " <table style='border: solid;width:" + opt.width + "px;border-color: #F3F3F3'>"
        table += "<tr style='height: 30px'><td>上传附件：</td><td> <input class='easyui-filebox' style='width: 230px' id='filCommon' name='filCommon'/><input type='hidden' id='hidGuid' name='hidGuid' /></td><td><a href='#' class='easyui-linkbutton' id='lbnUpLoad'  style='width: 60px;height:23px'>上传</a></td><td><a href='#' class='easyui-linkbutton' id='lbnDelete'  style='width: 60px;height:23px'>删除</a></td></tr>"
        table += "<tr id='trDatagrid'><td colspan='4'><table id='tbWlFiles' style='width:100%'></table></td></tr>";
        table += "</table>"
        table += "</form>"
        $(target).html("");
        $(target).append(table);
        $(target).find("#lbnUpLoad").linkbutton({
            onClick: function () {
                //  alert("aa");
                $(target).wlUpload("UploadFiles");
            }
        });


        $(target).find("#lbnDelete").linkbutton({
            onClick: function () {
                //  alert("aa");
                $(target).wlUpload("Delete");
            }
        });

        $(target).find("#filCommon").filebox({
            buttonText: "选择文件"
        });

        if (!opt.enabled) {
            $(target).find("#lbnUpLoad").linkbutton("disable");
            $(target).find("#lbnDelete").linkbutton("disable");
        }
    }


    $.fn.wlUpload = function (options, params) {
        if (typeof options === 'string') {
            return $(this).wlUpload.methods[options].call(this, params);
        }

        options = options || {};
        return this.each(function () {
            var opt = $.data(this, "wlUpload");
            if (opt) {
                $.extend(opt.options, options);
            } else {
                $.data(this, "wlUpload", {
                    options: $.extend({}, $.fn.wlUpload.defaults, options)
                });
            }
            init(this);
        });
    };




    $.fn.wlUpload.methods = {
        options: function () {
            return this.data().wlUpload.options;  //??
        },
        UploadFiles: function () {
            var opt = this.data().wlUpload.options;
            var target = $(this);

            target.find('#' + opt.guid).form('submit', {
                onSubmit: function (param) {
                    param.guid = opt.guid;
                },
                success: function (data) {
                    var data = JSON.parse(data);

                    if (data.state == "success") {
                        // alert("aa");
                        target.wlUpload("GetFileList");
                    }
                    else {
                        com.message(data.state, data.msg);
                    }
                }
            });
        },

        GetFileList: function () {
            var opt = this.data().wlUpload.options;
            var target = $(this);
            com.ajax({
                url: opt.fileUrl,
                type: opt.method,

                cache: false,

                data: { fileGuid: opt.guid },
                dataType: "json",
                success: function (data) {
                    if (data.length > 0) {
                        target.find("#trDatagrid").show();
                        target.find("#tbWlFiles").datagrid({
                            data: data,
                            loadMsg: '数据加载中请稍后……',
                            idField: "Id",
                            fitColumns: true,
                            singleSelect: true,
                            columns: [[
                    { field: 'BeforeName', title: '附件名称', align: "center" },
                    //{ field: 'Size', title: '大小', align: "center", width: 40 },
                    { field: 'FilleTime', title: '上传时间', align: "center", },
                    { field: 'UpFilePerson', title: '上传人', align: "center", },
                            {
                                field: 'Id', title: '下载', width: 40, align: "center", formatter: function (value, row, index) {
                                    return " <img src='../../Images/icon-download.jpg'  id='imgDownload' fileId='" + value + "'  />";
                                }
                            },
                            ]]
                        });
                        target.find("img").click(function () {
                            var id = $(this).attr("fileId");
                            //if (typeof ($.downloadIframe) == "undefined") {
                            //    var downIframe = document.createElement("downloadIframe");
                            //    $.downloadIframe = downIframe;
                            //    document.body.appendChild($.downloadIframe);
                            //    $.downloadIframe.src = "/Logic/CommonAjax/UploadAjax.aspx?method=DownloadFile&fileId=" + id;
                            //    $.downloadIframe.style.display = "none";
                            //}
                            var downIframe = document.createElement("iframe");
                            document.body.appendChild(downIframe);
                            downIframe.src = "/Logic/FileRelate/DownLoadFile.ashx?method=DownloadFile&fileId=" + id;
                            downIframe.style.display = "none";
                        });

                    } else {
                        target.find("#trDatagrid").hide();
                    }

                }
            });
        },

        enable: function () {
            var target = $(this);
            $(target).find("#lbnUpLoad").linkbutton("enable");
            $(target).find("#lbnDelete").linkbutton("enable");
        },
        disable: function () {
            var target = $(this);
            $(target).find("#lbnUpLoad").linkbutton("disable");
            $(target).find("#lbnDelete").linkbutton("disable");
        },
        GetGuid: function () {
            var opt = this.data().wlUpload.options;
            return opt.guid;
        },
        SetGuid: function (newGuid) {
            var opt = this.data().wlUpload.options;
            opt.guid = newGuid;
            return $(this);
        },
        Delete: function () {
            var opt = this.data().wlUpload.options;
            var target = $(this);
            var row = target.find("#tbWlFiles").datagrid("getSelected");
            if (row) {
                com.ajax(
                    {
                        url: opt.deleteUrl,
                        type: opt.method,
                        cache: false,
                        data: { fileId: row.Id },
                        dataType: "json",
                        success: function (data) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 5000,
                                showType: 'slide'

                            });
                            // console.log(target.find("#tbWlFiles"));
                            target.wlUpload("GetFileList");

                        }
                    }
                    );
            }
        },


    };



    $.fn.wlUpload.defaults = {
        width: 450,
        method: "post",
        guid: "",
        enabled: true,
        url: "/Logic/FileRelate/UploadAjax.aspx?method=UploadFiles",
        fileUrl: "/Logic/FileRelate/UploadAjax.aspx?method=GetFileList",
        deleteUrl: "/Logic/FileRelate/UploadAjax.aspx?method=DeleteFile",
    };

    if ($.parser) {
        $.parser.plugins.push('wlUpload');
    }

})(jQuery);